<?php
namespace frontend\controllers;

use common\models\category\Category;
use common\models\Fenhuigrade\Fenhuigrade;
use frontend\models\ArtForm;
use Yii;
use yii\filters\AccessControl;
use yii\filters\VerbFilter;
use yii\helpers\Url;
use yii\web\Controller;
use yii\web\Response;
use yii\base\ErrorException;
use yii\helpers\Html;
use common\models\member\Member;
use common\models\fenhuicat\Fenhuicat;
use common\models\fenhuiartic\Fenhuiartic;
use common\models\articles\Articles;
use yii\data\Pagination;
use yii\web\Request;

class FenhuiController extends Controller
{     
	public $layout = 'nomalpage';


	public function actionIndex($id){
		//分会列表
		$fenhuilist = Fenhuigrade::find()->where(['status'=> 10])->asArray()->All();
		$cat = Category::find()->where(['status'=>1,'id'=>$id])->asArray()->one();
		$article = Articles::find()->where(['status'=>10,'category_id'=>$id])->asArray()->one();
		return $this->render('index',[
			'article' =>$article,
			'cat' =>$cat,
			'fenhuilist' => $fenhuilist,
		]);
	}


    /**
    * 协会列表 zsn 20161229
    */
	/*    $tid     父栏目id
     *	  $id      当前分会id
     *	  $cid     当前栏目id
	*/
  public function actionList($id){
	 //分会列表

	  $fenhuilist = Fenhuigrade::find()->where(['status'=> 10])->asArray()->All();

	  //分会ID
	  if($id == 1){
		  $grade=Fenhuigrade::find()->where(['status'=>10])->orderBy('id ASC')->asArray()->one();
		  $id=$grade['id'];
	  }
	//分会名称
	  $fg=Fenhuigrade::findBySql("SELECT * FROM fenhuigrade where status = 10 and id = '$id '")->asArray()->one();


	  $plist = Fenhuicat::find()->where(['status'=>1,'fg_id'=>$id,'parent_id'=>0])->asArray()->All();
	  foreach ($plist as $key=>$pl){
		  $pid=$pl['id'];
		  $catlist =Fenhuicat::findBySql("SELECT * FROM fenhuicat where status = 1 and fg_id = '$id 'and parent_id = '$pid'")->asArray()->all();
		  $plist[$key]['zlm'] = $catlist;
	  }

	  if(isset(Yii::$app->request->get()['cid'])){
			$cid=Yii::$app->request->get()['cid'];
		  $gc = Fenhuicat::find()->where(['status'=>1,'fg_id'=>$id])->orderBy('id ASC')->asArray()->one();
	  }else{
		  $gc = Fenhuicat::find()->where(['status'=>1,'fg_id'=>$id])->orderBy('id ASC')->asArray()->one();
		  $cid=$gc['id'];
	  }

	  $ar = Fenhuiartic::find()->where(['status'=>10,'fg_id'=>$id,'fc_id'=>$cid])->orderBy('id ASC')->asArray()->one();

	  //相关新闻
	  $newslist = Articles::findBySql("SELECT * FROM articles where status = 10 and push = 1 and category_id = 2")->asArray()->limit(5)->all();

        return $this->render('list',[
         	'id' => $id,
         	'cid' => $cid,
         	'newslist' => $newslist,
         	'ar' => $ar,
         	'fg' => $fg,
			'fenhuilist' => $fenhuilist,
			'gc' => $gc,
			'plist' => $plist
        ]);
  }



}